package com.qk.management.service.serviceimpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.domain.ActivityDO;
import com.qk.dto.activity.ActivityDTO;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-12 21:05
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, ActivityDO> implements ActivityService {
    private static final int NOT_STARTED = 1;
    private static final int IN_PROGRESS = 2;
    private static final int FINISHED = 3;
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<ActivityDO> listPage(ActivityDTO activityDTO) {
        Page<ActivityDO> page = new Page<>(activityDTO.getPage(), activityDTO.getPageSize());
        LambdaQueryWrapper<ActivityDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtil.isEmpty(activityDTO.getChannel())) {
            queryWrapper.eq(ActivityDO::getChannel, activityDTO.getChannel());
        }
        if (!ObjectUtil.isEmpty(activityDTO.getType())) {
            queryWrapper.eq(ActivityDO::getType, activityDTO.getType());
        }
        if (!ObjectUtil.isEmpty(activityDTO.getStatus())) {
            switch (activityDTO.getStatus()) {
                case NOT_STARTED:
                    queryWrapper.gt(ActivityDO::getStartTime, LocalDateTime.now());
                    break;
                case IN_PROGRESS:
                    queryWrapper.le(ActivityDO::getStartTime, LocalDateTime.now())
                            .ge(ActivityDO::getEndTime, LocalDateTime.now());
                    break;
                case FINISHED:
                    queryWrapper.lt(ActivityDO::getEndTime, LocalDateTime.now());
                    break;
            }
        }
        Page<ActivityDO> pageResult = activityMapper.selectPage(page, queryWrapper);
        return PageResult.<ActivityDO>builder()
                .total(pageResult.getTotal())
                .rows(pageResult.getRecords())
                .build();
    }
}
